package com.demiroot.server.pages;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demiroot.server.HTTPResponse;
import com.demiroot.server.HTTPResquest;
import com.demiroot.server.HeaderData;
import com.demiroot.server.Server;
import com.demiroot.server.ServerUtils;
import com.demiroot.server.pages.JavaPageResponse;
import com.demiroot.server.pages.Page;


public class Folder implements Page {
	
	@Override
	public HTTPResponse getPage(HTTPResquest request) {
		try {
			return new JavaPageResponse(getPageString(request.getHeader(),request.getVars(),request.getServer()));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static boolean isThisYou(String file) {
		return file.equalsIgnoreCase("folder");
	}
	
	private String getPageString(HeaderData head, Map<String, String> vars, Server server) throws URISyntaxException, FileNotFoundException{
		String ret = "";
		
		File currentFolder = null;
		if (vars == null) vars = new HashMap<String, String>();
		String go = vars.get("goto");
		if (go != null) {
			if (go.endsWith("..")) {
				currentFolder = new File(go.replace("..", ""));
				currentFolder = currentFolder.getParentFile();
			} else {
				currentFolder = new File(go + "\\");
			}
		}
		
		
		String currentPath = "";
		String files = "";
		if (currentFolder != null) {
			if (currentFolder.list() == null) {
				ret += "<h1> Could not open folder: " + currentFolder.getAbsolutePath() + "</h1>";
				currentFolder = currentFolder.getParentFile();
			} else {
				
				files = "\t\t\tlist[0] = new File(\"..\", true)\n";
				List<File> fs = getFiles(currentFolder.listFiles());
				for (int x = 0; x < fs.size(); x++) {
					files += "\t\t\tlist[" + (x+1) + "] = new File(\"" + fs.get(x).getName() + "\"";
					if (fs.get(x).isFile()) {
						files += ", false)\n";
					} else {
						files += ", true)\n";
					}
				}
				currentPath=currentFolder.getAbsolutePath() + "\\";
			}
		} 
		
		if (currentFolder == null) {
			files =  "\t\t\tlist[0]=new File(\"..\", true)\n";
			files +=  "\t\t\tlist[1]=new File(\"C:\\\\\", true)\n";
			files += "\t\t\tlist[2]=new File(\"D:\\\\\", true);\n";
			files += "\t\t\tlist[3]=new File(\"E:\\\\\", true);\n";
			files += "\t\t\tlist[4]=new File(\"F:\\\\\", true);\n";
		}
		
		
		
		Map<String, String> swap = new HashMap<String, String>();
		swap.put("$CURRENT_PATH", currentPath);
		swap.put("$ARRAY_OF_FILES", files);
		ret += ServerUtils.generateFileFromTemplate("FolderText.template", swap, server.getConfig());
		
		return ret;
	}
	
	private List<File> getFiles(File[] fs) {
		List<File> folders = new ArrayList<File>();
		List<File> files = new ArrayList<File>();
		
		for (File f: fs) {
			if (f.isFile()) {
				folders.add(f);
			} else {
				files.add(f);
			}
		}
		
		Comparator<File> con = new Comparator<File>(){
			@Override
			public int compare(File o1, File o2) {
				return o1.getName().compareTo(o2.getName());
			}
		};
		
		Collections.sort(files, con);
		Collections.sort(folders, con);
		
		files.addAll(folders);
		
		return files;
	}

}
